/****** Drop Stored procedures if they exist ******/
IF EXISTS (SELECT*
  FROM sysobjects
  WHERE Id=OBJECT_ID( N'[dbo].[dimMergedItemExec]' )
		AND OBJECTPROPERTY( Id, N'IsProcedure' )=1)
BEGIN
DROP PROCEDURE [dbo].[dimMergedItemExec]
END

/****** Object:  StoredProcedure [dbo].[dimMergedItemExec]    Script Date: 06/09/2022 08:58:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[dimMergedItemExec] 
@Item [dbo].[ItemType] READONLY

AS 
BEGIN

IF  
--Check if the New sales pricing experience has been enabled
 (SELECT
      --QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.name) AS [TableName],
      SUM(sdmvPTNS.row_count) AS [RowCount]
FROM
      sys.objects AS sOBJ
      INNER JOIN sys.dm_db_partition_stats AS sdmvPTNS
            ON sOBJ.object_id = sdmvPTNS.object_id
WHERE 
      sOBJ.type = 'U'
      AND sOBJ.is_ms_shipped = 0x0
      AND sdmvPTNS.index_id < 2
	  AND sOBJ.name = 'stg$Price List Line'
GROUP BY
      sOBJ.schema_id
      , sOBJ.name
) > 0
BEGIN
		EXEC  [dbo].[v20dimMergedItem]
	END
ELSE IF
EXISTS (SELECT
		1
		FROM INFORMATION_SCHEMA.TABLES
		WHERE [TABLE_NAME] = 'stg$Retail Product Group')
	BEGIN
		EXEC  [dbo].[v15dimMergedItem]
	END
ELSE
	BEGIN
		EXEC  [dbo].[v9dimMergedItem]
	END
END
